<?php

include 'include/connection.php';
if(isset($_POST['action']))
{
	if($_POST['action'] == "list"){
		
		$start = "0";
		$echo = "1";

		if(isset($_POST['start']))
			$start = $_POST['start'];

		if(isset($_POST['echo']))
			$echo = $_POST['echo'];
			
		if(isset($_POST['sort']))
			$sort = $_POST['sort'];
			
		if(isset($_POST['sortdir']))
			$sortdir = $_POST['sortdir'];
			
		if(isset($_POST['texto']))
			$texto = $_POST['texto'];
			
		$length = "10";
		$itotalRecord = "0";

		$columnsSort = array("", "fname", "pname", "mail", "reg_date","login");

		$query = "select * from user where role = 'user' ";
		$query_count = "select count(*) from user where role = 'user' ";

		if(isset($texto) && !$texto == "")
		{
			$query = $query . "and (fname like '%" . $texto . "%' or pname like '%" .$texto. "%' or mail like '%". $texto."%') ";
			$query_count = $query_count . "and (fname like '%" . $texto . "%' or pname like '%" .$texto. "%' or mail like '%". $texto."%') ";
		}

		if(isset($sort) && isset($sortdir))
			$query = $query . "order by ". $columnsSort[$sort] ." ".$sortdir ." ";
			
		$query = $query .'limit '. $start .','.$length .' ';

		$result = mysql_query($query);
		$result2 = mysql_query($query_count);
		$count = mysql_fetch_array($result2, MYSQL_BOTH);
		$count = $count[0];

		if($length > $count)
			$itotalRecord = $length;
		else $itotalRecord = $count;

		$data = array();

		while ($row = mysql_fetch_array($result, MYSQL_BOTH))
		{
			$arr = array();
			array_push($arr,"<input type=\"checkbox\" name=" . $row["id_user"] . "></input>");
			array_push($arr,$row["fname"]);
			array_push($arr,$row["pname"]);
			array_push($arr,$row["mail"]);
			array_push($arr,date('m/d/Y', strtotime($row["reg_date"])));
			array_push($arr,$row["login"]);
			array_push($arr,'<a href="profile.php?id='. $row["id_user"] . '"><img src="images/members.png" alt="view" /></a>');
			
			array_push($data,$arr);
		}
		header("Content-type:application/json");
		$json = json_encode(array('iTotalDisplayRecords' => $itotalRecord,'iTotalRecords' => $count,'aaData' => $data,'sEcho' => $echo));
		echo $json;
	}
	else if($_POST['action'] == "delete")
	{
		if(isset($_POST['ids']))
		{
			$ids = $_POST['ids'];
			$idsArr = explode(",",$ids);
			foreach($idsArr as $id)
			{
				if($id != "" && $id != ",")
				{
					$query = "delete from user where id_user = ". $id;
					$result = mysql_query($query);
				}
			}

		}
		
	}
}

?>